<?php
class Product_Model_ProductStocket
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Product_Model_DbTable_ProductStocket();
	}
	
	public static function getInstance(){
		return new Product_Model_ProductStocket();
	}
	
	public function getAdapter(){
		return $this->_table->getAdapter(); 
	}
	/**
	*Create A Record  
	*@param rowSet $row product_stocket's data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row product_stocket's data 
	*@param int $product_id 
	*@return boolean
	*/
	public  function update($row,$id,$warehouseId=1)
	{
		$row['ps_last_update'] = date("Y-m-d H:i:s");
		$where = $this->_table->getAdapter()->quoteInto('product_id = ?', $id);
		$where .= $this->_table->getAdapter()->quoteInto(' and warehouse_id = ?', $warehouseId);
		return $this->_table->update($row, $where);	
	}
		
	/**
	*No Delete One Row
	*@param int $product_id 
	*@return boolean
	*/
	 public function delete($id,$warehouseId=1){
		$where = $this->_table->getAdapter()->quoteInto('product_id = ?', $id); 
		$where .= $this->_table->getAdapter()->quoteInto(' and warehouse_id = ?', $warehouseId);
		return $this->_table->delete($where);
	}
	
	public function getByProductId($id,$warehouseId=1){	
		$where = $this->_table->getAdapter()->quoteInto('product_id = ?', $id); 
		$where .= $this->_table->getAdapter()->quoteInto(' and warehouse_id = ?', $warehouseId);
		if($rowset = $this->_table->fetchRow($where)){
			return $rowset->toArray();
		}	
		return false;			
	}
	
    public function getByCondition($condition=array(),$type='*',$orderby="product_id",$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where(" product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["product_sku"]) && $condition["product_sku"]!=''){
        	$tempProduct=array(
        			'product_sku'=>$condition["product_sku"]
        			);
        	$productRow=Product_Service_Product::getByCondition($tempProduct,"*");
        	if($productRow){
        		$select->where(" product_id = ?",$productRow[0]['product_id']);
        	}
        }
        if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
        	$tempProduct=array(
        			'customer_id'=>$condition["customer_id"]
        	);
        	$productRow=Product_Service_Product::getByCondition($tempProduct,"*");
        	if($productRow){
        		$strInProductids="";
        		foreach ($productRow as $v){
        			$arrInProductids[]=$v['product_id'];
        		}
        		$strInProductids=implode(",",$arrInProductids); 
        		$select->where(" product_id in(?)",$strInProductids);
        	}
        	
	
        }
		
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
			
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}	
	/**
	 * 出货更新冻结库存量
	 * 
	 */
	public  function updateQuantityHold($row,$awhere){
		//$strWhere=implode(' AND ',$awhere);
		
		$where = $this->_table->getAdapter()->quoteInto('1= ?', 1);
		foreach ($awhere as $key=>$val){
			$where .=$this->_table->getAdapter()->quoteInto(" AND {$key} = ?", $val);
		}
		return $this->_table->update($row, $where);
	}
	
	/**
	 *  根据多个条件更新指定数据
	 */
	public function updateByCond(array $row,array $cond)
	{
		$adapter = $this->_table->getAdapter();
		$where = $adapter->quoteInto(" product_id= ?", $cond['product_id']);
		$where .= $adapter->quoteInto(" and warehouse_id= ?", $cond['warehouse_id']);
		return $this->_table->update($row, $where);
	}
	
}